/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusInnerService;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TabPanel;

/**
 * 
 * @Author Kent
 */
public class BusDetailContent extends Content {
	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private String busId = null;
	private TabPanel panel;
	private GwtContext context;

	// public void setContentChangeListener(ContentChangeListener l){
	// s.setContentChangeListener(l);
	// cmc.setContentChangeListener(l);
	// }

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			ArrayList ret = (ArrayList) res.getResponse();
			ServiceBus bus = (ServiceBus) ret.get(0);
			ArrayList bindings = (ArrayList) ret.get(1);
			BusInnerService in = (BusInnerService) ret.get(2);
			Boolean isLocal = (Boolean) ret.get(3);
			BusDetailSubContent s = new BusDetailSubContent(cons, service,
					busId, context, bus, bindings);
			s.setContentChangeListener(listener);
			panel.add(s, cons.overview());

			BusInnerServiceContent inc = new BusInnerServiceContent(cons,
					service, in);
			panel.add(inc, cons.innerService());

			if (isLocal) {
				BusMonitorContent cmc = new BusMonitorContent(cons, service,
						bus.getCode(), context);
				cmc.setContentChangeListener(listener);
				panel.add(cmc, cons.monitor());
			}

			panel.selectTab(0);

		}
	};

	public BusDetailContent(DomainMessages cons, BifangGWTServiceAsync service,
			String busId, GwtContext context) {
		this.cons = cons;
		this.service = service;
		this.busId = busId;
		this.context = context;

		panel = new TabPanel();
		panel.setWidth("100%");

		initWidget(panel);

	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		req.setRequest(this.busId);
		req.setAction("han.bifang.gwt.server.action.domain.GetBusDetailAction");
		service.call(req, cb);

	}

}
